/**
 * Project: jbpm4Test
 * Copyright 2014  Top Digital Technology  Co. Ltd.
 * All right reserved.
 */
package com.topdt.jbpm.extension;

import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.Activity;
import org.jbpm.api.model.Transition;
import org.jbpm.pvm.internal.model.ActivityImpl;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 建立日期 : 2014年11月26日 上午11:18:27<br>
 * 作者 : balance<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
public class ForeachListener implements EventListener {

	private static final long serialVersionUID = 435895238582692796L;
	private static Logger log = LoggerFactory.getLogger(ForeachListener.class);

	public void notify(EventListenerExecution execution) throws Exception {
		ExecutionImpl exe = (ExecutionImpl) execution;

		ActivityImpl act = exe.getActivity();

		Transition out = act.getOutgoingTransitions().get(0);
		Activity destAct = out.getDestination();
		String actName = destAct.getName();


		String users = (String) exe.getVariable(actName + "_assginee");
		exe.removeVariable(actName + "_assginee");
		
		log.debug("动态创建分支，取得分支上第一个节点的处理人："+users);

		exe.setVariable("foreach_users", users);

		int quorum = users.split(",").length;

		exe.setVariable("foreach_quorum", quorum);
	}

}
